package com.hbk.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hbk.base.util.StringUtil;
import com.hbk.base.util.TimeUtil;
import com.hbk.entity.*;
import com.hbk.mapper.*;
import com.hbk.service.ICacheService;
import com.hbk.service.IWebService;
import com.hbk.vo.IndexVo;
import com.hbk.vo.NavVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WebServiceImpl implements IWebService {


    @Resource
    private NewsMapper newsMapper;
    @Resource
    private CarouselMapper carouselMapper;
    @Resource
    private ConsultationMapper consultationMapper;
    @Resource
    private SchoolItemMapper schoolItemMapper;
    @Resource
    private FriendLinkMapper friendLinkMapper;
    @Resource
    private NewsContentMapper newsContentMapper;
    @Resource
    private NewsAttachmentMapper newsAttachmentMapper;
    @Resource
    private NavigationMapper navigationMapper;

    @Resource
    private ICacheService cacheService;


    /** 获取导航的数据 */
    public void getNav(HttpServletRequest request){
        String cache = cacheService.getNavCache();
        NavVo vo = null;
        if(StringUtil.isEmpty(cache)){
            vo = new NavVo();
            List<Navigation> list = navigationMapper.listAll();
            for(Navigation navigation : list){
                NavVo.NavItemVo itemVo = new NavVo.NavItemVo();
                itemVo.setName(navigation.getName());
                itemVo.setUrl(navigation.getUrl());
                String id = navigation.getId();
                if(id.equals("1")){
                    vo.setIndex(itemVo);
                }else if(id.equals("2")){
                    vo.setGncd(itemVo);
                }else if(id.equals("3")){
                    vo.setLine(itemVo);
                }else if(id.equals("4")){
                    vo.setRecruit(itemVo);
                }else if(id.equals("5")){
                    vo.setYxdy(itemVo);
                }else if(id.equals("6")){
                    vo.setAbout(itemVo);
                }

                else if(id.equals("21")){
                    vo.getGncdList().add(itemVo);
                }else if(id.equals("22")){
                    vo.getGncdList().add(itemVo);
                }else if(id.equals("23")){
                    vo.getGncdList().add(itemVo);
                }else if(id.equals("24")){
                    vo.getGncdList().add(itemVo);
                }else if(id.equals("25")){
                    vo.getGncdList().add(itemVo);
                }
            }
            cache = JSON.toJSONString(vo);
            cacheService.setNavCache(cache);
        }else {
            vo = JSON.parseObject(cache, NavVo.class);
        }
        request.setAttribute("nav", vo);
    }

    public void index(HttpServletRequest request){

        String cache = cacheService.getIndexCache();
        IndexVo vo = null;
        if(StringUtil.isEmpty(cache)){
            vo = new IndexVo();
            indexNews(vo);
            indexCarouse(vo);
            indexConsultation(vo);
            indexSchoolItem(vo);
            indexFriendLink(vo);
            cache = JSON.toJSONString(vo);
            cacheService.setIndexCache(cache);
        }else {
            vo = JSON.parseObject(cache, IndexVo.class);
        }
        request.setAttribute("index", vo);
    }

    private void indexNews(IndexVo vo){
        List<String> newsIdList = newsMapper.newsLogicOrder(0, 7);
        if(newsIdList.size()==0){
            return;
        }
        List<News> newsList = newsMapper.selectBatchIds(newsIdList);
        Map<String, News> newsMap = new HashMap<>();
        for(News news : newsList){
            newsMap.put(news.getId(), news);
        }

        for(int i=0; i<newsIdList.size(); i++){
            String newsId = newsIdList.get(i);
            News news = newsMap.get(newsId);
            IndexVo.NewsVo newsVo = new IndexVo.NewsVo();
            newsVo.setId(news.getId());
            newsVo.setTitle(news.getTitle());
            newsVo.setSummary(news.getSummary());
            if(i<2){
                vo.getNewsList1().add(newsVo);
            }else{
                vo.getNewsList2().add(newsVo);
            }
        }
    }

    private void indexCarouse(IndexVo vo){
        List<Carousel> list = carouselMapper.listAll();
        int i = 0;
        for(Carousel carousel : list){
            IndexVo.CarouselVo carVo = new IndexVo.CarouselVo();
            carVo.setId(carousel.getId());
            carVo.setTitle(carousel.getTitle());
            carVo.setImgUrl(carousel.getImgUrl());
            carVo.setHasLink(carousel.getHasLink());
            carVo.setLinkUrl(carousel.getLinkUrl());
            carVo.setDataIndex(i);
            vo.getCarouselList().add(carVo);
            i++;
        }
    }

    private void indexConsultation(IndexVo vo){
        List<Consultation> list = consultationMapper.list(0, 5);
        for(int i=1; i<=list.size(); i++){
            Consultation consultation = list.get(i-1);
            IndexVo.ConsultationVo conVo = new IndexVo.ConsultationVo();
            conVo.setId(consultation.getId());
            conVo.setImgUrl(consultation.getImgUrl());
            if(i==1){
                vo.setConsul1(conVo);
            }else if(i==2){
                vo.setConsul2(conVo);
            }else if(i==3){
                vo.setConsul3(conVo);
            }else if(i==4){
                vo.setConsul4(conVo);
            }else if(i==5){
                vo.setConsul5(conVo);
            }
        }
    }

    private void indexSchoolItem(IndexVo vo){
        List<SchoolItem> schoolItems = schoolItemMapper.listAll();
        for(SchoolItem item : schoolItems){
            IndexVo.SchoolItemVo itemVo = new IndexVo.SchoolItemVo();
            itemVo.setId(item.getId());
            itemVo.setTitle(item.getTitle());
            itemVo.setImgUrl(item.getImgUrl());
            itemVo.setLinkUrl(item.getLinkUrl());
            vo.getSchoolItemList().add(itemVo);
        }
    }

    private void indexFriendLink(IndexVo vo){
        List<FriendLink> friendLinks = friendLinkMapper.listAll();
        for(FriendLink link : friendLinks){
            IndexVo.FriendLinkVo linkVo = new IndexVo.FriendLinkVo();
            linkVo.setId(link.getId());
            linkVo.setTitle(link.getTitle());
            linkVo.setUrl(link.getUrl());
            vo.getFriendLinkList().add(linkVo);
        }
    }

    public void about(HttpServletRequest request){
        String contentId = "gywm";
        NewsContent content = newsContentMapper.selectById(contentId);
        if(content==null){
            return;
        }
        request.setAttribute("about_content", content.getContent());
    }

    public void concat(HttpServletRequest request){

        String contentId = "lxwm";
        NewsContent content = newsContentMapper.selectById(contentId);
        if(content==null){
            return;
        }
        request.setAttribute("concat_content", content.getContent());
    }

    public void increment(HttpServletRequest request){
        String contentId = "zzfw";
        NewsContent content = newsContentMapper.selectById(contentId);
        if(content==null){
            return;
        }
        request.setAttribute("increment_content", content.getContent());
    }

    /** 参会报名 */
    public void examination(HttpServletRequest request){
        String contentId = "bmch";
        NewsContent content = newsContentMapper.selectById(contentId);
        if(content==null){
            return;
        }
        request.setAttribute("examination_content", content.getContent());
        List<NewsAttachment> newsAttachments = newsAttachmentMapper.listByContentId(contentId);
        if(newsAttachments!=null && newsAttachments.size()>0){
            JSONArray array = new JSONArray();
            for(NewsAttachment attachment : newsAttachments){
                JSONObject obj = new JSONObject();
                obj.put("url", attachment.getUrl());
                int i = attachment.getName().lastIndexOf(".");
                if(i<0){
                    obj.put("name", attachment.getName());
                }else{
                    obj.put("name", attachment.getName().substring(0, i));
                }
                array.add(obj);
            }
            request.setAttribute("attachments", array);
        }
    }

    /** 线路安排 */
    public void line(HttpServletRequest request){
        String contentId = "xlap";
        NewsContent content = newsContentMapper.selectById(contentId);
        if(content==null){
            return;
        }
        request.setAttribute("line_content", content.getContent());
    }

    /** 新闻详情 */
    public boolean news(String newsId, HttpServletRequest request){
        if(StringUtil.isEmpty(newsId)){
            return false;
        }
        News news = newsMapper.selectById(newsId);
        if(news==null){
            return false;
        }
        request.setAttribute("news_type", news.getType());
        request.setAttribute("news_title", news.getTitle());
        request.setAttribute("news_time", TimeUtil.formaterYYYYMMdd(news.getPublishTime()));

        NewsContent newsContent = newsContentMapper.getByNewsId(newsId);
        if(newsContent!=null){
            request.setAttribute("news_content", newsContent.getContent());
        }
        return true;
    }
}
